//
//  PVDDetailChecklistSelfieCell.swift
//  ezyform
//
//  Created by Moennig Q on 5/9/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class PVDDetailChecklistSelfieCell: UITableViewCell {
    
    let selfieImageView = UIImageView()
    lazy var titleLabel: ContentLabel = {
        let label = ContentLabel(R.font.interSemiBold(size: 16),hexStr: "#292D32FF")
        label.numberOfLines = 0
        return label
    }()
    lazy var resultLabel: ContentLabel = {
        let label = ContentLabel(R.font.interMedium(size: 14),hexStr: "#63666AFF")
        label.numberOfLines = 0
        return label
    }()
    lazy var lockImageView: UIImageView = {
        return UIImageView(image: UIImage(named: "installation_detail_checklist_lock"))
    }()
    lazy var uploadFlagImageView: UIImageView = {
        let image = UIImageView()
        return image
    }()
    let isRequiredImageView = UIImageView(image: UIImage(named: "checkListCellStar"))
    let moreImageView = UIImageView(image: UIImage(named: "pvdlist_icon_more"))
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpviews()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    func setUpviews(){
        self.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        self.contentView.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        self.selectionStyle = .none
        
        //back view
        let backView = CustomBackView()
        self.contentView.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(BaseChecklistTableView.checklistCellHeight)
            make.bottom.equalToSuperview()
        }
        //selfie image view
        backView.addSubview(selfieImageView)
        selfieImageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.width.height.equalTo(30)
            make.centerY.equalToSuperview()
        }
        //uploadFlagImageView,是否上传成功的标记，红色感叹号是本地数据没有上传，绿色勾表示上传成功
        backView.addSubview(uploadFlagImageView)
        uploadFlagImageView.snp.makeConstraints { (make) in
            make.bottom.equalTo(selfieImageView)
            make.right.equalTo(selfieImageView).offset(3)
            make.size.equalTo(CGSize(width: 16, height: 11))
        }
        //
        backView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(selfieImageView.snp.right).offset(12)
            make.centerY.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)

        }
        //isRequiredImageView
        backView.addSubview(isRequiredImageView)
        isRequiredImageView.snp.makeConstraints { make in
            make.top.equalTo(selfieImageView).offset(6)
            make.left.equalTo(selfieImageView.snp.right).offset(4)
            make.height.width.equalTo(7)
        }
        //more image view
        moreImageView.contentMode = .center
        backView.addSubview(moreImageView)
        moreImageView.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
        }
        //result label
        //抗压缩,抗拉伸能力最强，不会被压缩或拉伸
        resultLabel.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        resultLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        backView.addSubview(resultLabel)
        resultLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right)
            make.right.equalTo(moreImageView.snp.left)
            make.height.width.greaterThanOrEqualTo(0)
            make.centerY.equalToSuperview()
        }
        //lockImageView
        backView.addSubview(lockImageView)
        lockImageView.snp.makeConstraints { make in
            make.right.equalTo(moreImageView.snp.left)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
    }
    func setData(_ model: PVDDetailModel,index: Int){
        guard index < model.pvd_checklists.count else { return }
        let checkModel = model.pvd_checklists[index]
        
        self.lockImageView.isHidden = true
        self.titleLabel.text = checkModel.title
        
        if checkModel.is_required == "true"{
            isRequiredImageView.isHidden = false
        }else{
            isRequiredImageView.isHidden = true
        }
        let serverModel = model.pvd_checklists[index]
        //是否显示本地数据标识
        let imagesCnt =  serverModel.images.count
        if imagesCnt == 0 {
            selfieImageView.image = R.image.pvdDetail_checklist_selfie_nodata()//UIImage(named: "PVDDetail_checklist_selfie_nodata")
        }else{
            selfieImageView.image =  R.image.pvdDetail_checklist_selfie_withdata()
            self.uploadFlagImageView.image = (serverModel.haveLocalDataFlag ? R.image.uploadImageFailed() : nil)
        }
        //如果有数据，标识此checklist已经完成
        let checklistID = serverModel.id
        let minImageCount = serverModel.minimum_quantity
        //如果当前checklist是mid-installation check-up或者testing and commissioning checklist而且上一步没有完成
        if (checklistID == ChecklistFixedId.midInstall.rawValue && !JobDetailModelHelper.shared.jobDetailModel.checkInSelfieDoneFlag) || (checklistID == ChecklistFixedId.testAndCommission.rawValue && !JobDetailModelHelper.shared.jobDetailModel.midInstallationSelfieDoneFlag){
            self.lockImageView.isHidden = false
            self.resultLabel.text = ""
        }else{
            
            if imagesCnt > 0{//已经有图片，标识此步骤已经完成了
                self.lockImageView.isHidden = true
                if minImageCount > 0{//只有设置了最小image cnt的才显示这样的格式
                    self.setMinImagesCount(imagesCnt, minImageCount: minImageCount)
                }else{
                    self.resultLabel.text = "#\(imagesCnt)"
                    self.resultLabel.textColor = UIColor(hexStr: "#63666A")
                }
            }else{//没有图片，标识此步骤未开始，需要检验下此步骤之前的checklist是否完成
                if minImageCount > 0{
                    self.setMinImagesCount(0, minImageCount: minImageCount)
                }else{
                    self.resultLabel.text = ""
                }
            }
        }
    }
    func setMinImagesCount(_ imageCnt: Int, minImageCount: Int64){
        let subStr = "\(imageCnt)"
        let content = subStr + "/\(minImageCount)"
        let attrString = NSMutableAttributedString(string: content )
        let attr1: [NSMutableAttributedString.Key: Any] = [.foregroundColor: UIColor(hexStr: "#63666A")]
        let attr2: [NSMutableAttributedString.Key: Any] = [.foregroundColor: UIColor(hexStr: "#379657")]
        
        attrString.addAttributes(attr1, range: NSRange(location: 0, length: attrString.length))
        attrString.addAttributes(attr2, range: NSRange(location: 0, length: subStr.count))
        self.resultLabel.attributedText = attrString
    }
}
